#include "stdafx.h"
#include "CommonPlatformUtilities.h"


CommonPlatformUtilities::CommonPlatformUtilities()
{

	NOTIMPL

}

XCHAR * CommonPlatformUtilities::Xstralloc(const XCHAR * sourceString, const XUINT32 sourceStringLen)
{

	XCHAR * result;

	if ( sourceStringLen <= 0x7FFFFFFE )
	{
		XCHAR * theNewString = new XCHAR[2 * (sourceStringLen + 1)];

		if ( theNewString )
		{
			*(theNewString + sourceStringLen) = NULL;
			if ( sourceString )
				this->Xstrncpy(theNewString,sourceString,sourceStringLen);
		}
		result = theNewString;
	}
	else
	{
		result = NULL;
	}
	return result;

}

void CommonPlatformUtilities::Xstrfree(XCHAR * theString)
{

	NOTIMPL

}

void * CommonPlatformUtilities::Xstrncpy(XCHAR * pTrg, const XCHAR * pSrc, XUINT32 cChar)
{

	XCHAR cch;
	void *pStart;

	pStart = pTrg;
	while ( cChar )
	{
		cch = *pSrc;
		++pSrc;
		*pTrg = cch;
		++pTrg;
		if ( !cch )
			break;
		--cChar;
	}
	return pStart;
}

XUINT32 CommonPlatformUtilities::Xstrlen(const XCHAR * pString)
{

	XUINT32 result; 
	XINT32 cch; 
	const XCHAR * pEos; 

	if ( pString )
	{
		pEos = pString;
		do
		{
			cch = *pEos;
			++pEos;
		}
		while ( cch );
		result = pEos - pString - 1;
	}
	else
	{
		result = 0;
	}

	return result;

}

XINT32 CommonPlatformUtilities::Xstrncmpi(const XCHAR * pTrg, const XCHAR * pSrc, XUINT32 cChar)
{

	WCHAR cchTrg;
	WCHAR cchSrc;
	int ret;

	while ( cChar )
	{
		if ( (XINT32)*pTrg >= 128 || (XINT32)*pSrc >= 128 )
		{
			if ( *pTrg != *pSrc )
				break;
		}
		else
		{
			cchTrg = *pTrg;
			cchSrc = *pSrc;
			if ( (XUINT8)(cchTrg & 223) != (XUINT8)(cchSrc & 223) )
				break;
		}
		++pTrg;
		++pSrc;
		--cChar;
	}
	if ( cChar )
		ret = *pTrg - *pSrc;
	else
		ret = 0;
	return ret;

}

XINT32 CommonPlatformUtilities::Xstrncmp(const XCHAR * pTrg, const XCHAR * pSrc, XUINT32 cChar)
{

	XINT32 ret;

	while ( cChar && *pTrg == *pSrc )
	{
		++pTrg;
		++pSrc;
		--cChar;
	}
	if ( cChar )
		ret = *pTrg - *pSrc;
	else
		ret = 0;
	return ret;

}
